package com.gzwanchuang.xhgj.ui;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.navigation.NavController;
import androidx.navigation.NavDestination;
import androidx.navigation.Navigation;

import com.google.protobuf.InvalidProtocolBufferException;
import com.gzwanchuang.architecture.utils.APKVersionCodeUtils;
import com.gzwanchuang.architecture.utils.StatusBarUtil;
import com.gzwanchuang.xhgj.R;
import com.gzwanchuang.xhgj.bridge.data.repository.Apis;
import com.gzwanchuang.xhgj.help.updateutil.UpdateManager;
import com.gzwanchuang.xhgj.net.NetworkManager;
import com.gzwanchuang.xhgj.net.interfaces.OnRequestCallBack;
import com.gzwanchuang.xhgj.proto.Index;
import com.gzwanchuang.xhgj.ui.base.BaseActivity;
import com.huawei.hms.hmsscankit.ScanUtil;
import com.huawei.hms.ml.scan.HmsScan;
import com.tencent.ocr.sdk.common.OcrModeType;
import com.tencent.ocr.sdk.common.OcrSDKConfig;
import com.tencent.ocr.sdk.common.OcrSDKKit;
import com.tencent.ocr.sdk.common.OcrType;

public class MainActivity extends BaseActivity {

    private long mExitTime;
    private NavController nav;
    private  final int REQUEST_CODE_SCAN = 0X01;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        StatusBarUtil.setStatusBarLightMode(this, Color.parseColor("#FFFFFF"), true);
        nav = Navigation.findNavController(this, R.id.main_fragment_host);
        nav.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
            @Override
            public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {
               if (destination.getLabel().equals("fragment_home_main"))
               {
                   mSharedViewModel.homeLoad.setValue(true);
               }
            }
        });

        checkVersion();
    }

    @Override
    public void onBackPressed() {
        if (nav.getCurrentDestination() != null) {
            if (nav.getCurrentDestination().getId() == R.id.mainRootFragment && (System.currentTimeMillis() - mExitTime) > 2000) {
                showShortToast("再按一次退出程序");
                mExitTime = System.currentTimeMillis();
            } else {
                super.onBackPressed();
            }
        }
    }



    private void checkVersion() {
        Index.get_app_version_info.Builder builder = Index.get_app_version_info.newBuilder();
        builder.setClient("android");
        NetworkManager.INSTANCE.post(Apis.versionCode, builder.build().toByteArray(), new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) {
                checkVerison(response);
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                hideProgress();
            }
        });

    }

    private void checkVerison(byte[] response) {
        String versionName = APKVersionCodeUtils.getVerName(this);
        Index.get_app_version_info versionBean = null;
        try {
            versionBean = Index.get_app_version_info.parseFrom(response);
        } catch (InvalidProtocolBufferException e) {
            e.printStackTrace();
        }
        Log.e("mango", "checkVerison: " + versionBean);
        // 改为匹配版本号
        float serviceVersion = Float.parseFloat(versionBean.getVersion());
        float locationeVersion = Float.parseFloat(versionName);
        if (locationeVersion < serviceVersion) {
            UpdateManager.getInstance()
                    .setContext(this)
                    .setFragmentManager(getSupportFragmentManager())
                    .setUrl(versionBean.getDownloadUrl())
                    .setIsforce(versionBean.getForce())
                    .setContent(versionBean.getContext())
                    .start();
        }
    }



    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        //当扫码页面结束后，处理扫码结果
        //当扫码页面结束后，处理扫码结果
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK || data == null) {
            return;
        }
        mSharedViewModel.sn.setValue(data.getStringExtra(ScanUtil.RESULT));
    }

}
